﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.filters.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.controllers.startup.*;
    import net.wooga.pets.constants.windows.*;

    public class SpeechBubbleDisplayMediator extends RenderListMediator {

		[Inject]
        public var speechbubbleDisplay:SpeechBubbleDisplay;
		[Inject]
        public var gameViewModel:GameViewModel;
        private var tickComponent:ITicking;
        private var componentId:String;

        override public function onRegister():void{
            this.speechbubbleDisplay.buttonMode = true;
            this.speechbubbleDisplay.mouseChildren = false;
            eventMap.mapListener(eventDispatcher, EventConstants.ADD_SPEECHBUBBLE_TO_LAYER, this.addSpeechBubble);
            eventMap.mapListener(eventDispatcher, EventConstants.CHECK_REMOVE_SPEECHBUBBLE_FROM_LAYER, this.checkRemoveSpeechBubble);
            eventMap.mapListener(eventDispatcher, EventConstants.REMOVE_SPEECHBUBBLE_FROM_LAYER, this.removeSpeechBubble);
            eventMap.mapListener(eventDispatcher, WindowsEventConstants.ON_WINDOW_VIEW_ADDED_TO_STAGE, this.removeSpeechBubble);
            eventMap.mapListener(eventDispatcher, WindowsEventConstants.NOTIFICATION_VIEW_ADDED_TO_STAGE, this.removeSpeechBubble);
            eventMap.mapListener(eventDispatcher, GameEvent.LEVEL_UP, this.removeSpeechBubble);
        }
        private function addSpeechBubble(_arg1:InfoEvent):void{
            this.removeSpeechBubble();
            var _local2:MovieClip = (_arg1.info.mc as MovieClip);
            var _local3:Point = (_arg1.info.pos as Point);
            var _local4:BitmapFilter = (_arg1.info.mouseOverEffect as BitmapFilter);
            _local3 = this.speechbubbleDisplay.globalToLocal(_local3);
            _local2.x = _local3.x;
            _local2.y = _local3.y;
            if (_local4){
                _local2.filters = [_local4];
            };
            var _local5:Number = (1.5 / this.gameViewModel.gameViewScale);
            if (_arg1.info.zoom){
                _local2.scaleX = (_local2.scaleY = _local5);
            };
            this.speechbubbleDisplay.addChild(_local2);
        }
        private function checkRemoveSpeechBubble(_arg1:InfoEvent):void{
            var _local2:IActing = (_arg1.info.vo as IActing);
            this.componentId = _local2.id;
            var _local3:ITicking = (_arg1.info.component as ITicking);
            if (((this.tickComponent) && (!((_local3 == this.tickComponent))))){
                dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this.tickComponent));
            };
            this.tickComponent = _local3;
            this.onMouseOut(null, this.tickComponent);
        }
        private function onMouseOut(_arg1:Event, _arg2:ITicking=null):void{
            var _local3:Boolean = this.speechbubbleDisplay.hitTestPoint(this.speechbubbleDisplay.stage.mouseX, this.speechbubbleDisplay.stage.mouseY);
            if (_local3){
                this.speechbubbleDisplay.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseOut);
                this.speechbubbleDisplay.addEventListener(Event.MOUSE_LEAVE, this.onMouseOut);
                this.speechbubbleDisplay.addEventListener(MouseEvent.CLICK, this.onClick);
            } else {
                this.tickComponent = ((_arg2) || (this.tickComponent));
                dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this.tickComponent));
                dispatch(new InfoEvent(EventConstants.REMOVE_MOUSE_OVER_EFFECT, this.componentId));
                this.speechbubbleDisplay.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseOut);
                this.speechbubbleDisplay.removeEventListener(Event.MOUSE_LEAVE, this.onMouseOut);
                this.speechbubbleDisplay.removeEventListener(MouseEvent.CLICK, this.onClick);
                this.removeSpeechBubble();
            };
        }
        private function onClick(_arg1:MouseEvent):void{
            dispatch(new InfoEvent(EventConstants.CLICK_ON_SPEECHBUBBLE, this.componentId, _arg1));
        }
        private function removeSpeechBubble(_arg1:Event=null):void{
            Displays.removeAllChildren(this.speechbubbleDisplay);
        }

    }
}//package net.wooga.pets.views 
